import * as types from './types'
import store from '../store/store'
import {GetNofiticationsApi} from '../../config/api'

//请求homeworks
export const fetch_notifications = (filter=null, created_at="") => {
    return dispatch => {
        dispatch({type: types.FETCHING_NOTIFICATION})
        const notification_reducer = store.getState().notification
        GetNofiticationsApi(notification_reducer.current_page, filter, created_at).then((data) => {
            if (data.code === 0) {
                //通知reducer请求成功
                dispatch({type: types.FETCH_NOTIFICATION_SUCCESS, notifications: data.notifications})
                if (data.notifications.length < notification_reducer.per) { dispatch({type: types.NOTIFICATION_END_OF_PAGE})} //通知reducer已经没有数据了
            } else {
                //通知reducer请求失败
                dispatch({type: types.FETCH_NOTIFICATION_FAIL})
            }
        }).catch((err) => {
           //通知reducer请求失败
           dispatch({type: types.FETCH_NOTIFICATION_FAIL})
        })
    }
}

//刷新
export const refresh_notifications = (filter=null, created_at="") => {
    return dispatch => {
        dispatch({type: types.REFRESH_NOTIFICATIONS})
        dispatch(fetch_notifications(filter, created_at))
     }
}

//更新其中一条数据，从而刷新列表
export const update_notification = (id, param={}) => {
    return {type: types.UPDATE_NOTIFICATION, id: id, param: param}
}
